/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.model.dao;
import hibernate.pojo.Khachhang;



import hibernate.util.HibernateUtil;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
/**
 *
 * @author phathuynh
 */
public class KhachHangDAO 
{
    
    //<editor-fold defaultstate="collapsed" desc="1. Lấy khách hàng by ID">
    
     public static Khachhang getCustomerByID(int customerID){
         Khachhang customer = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try{
              customer = (Khachhang) session.get(Khachhang.class, customerID);
         } catch(HibernateException ex){
             System.err.print(ex);
         } finally{
             session.close();
         }
         return customer;
     }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy khách hàng ID by account">
     public static int getCustomerIDByAccount(String taiKhoan){
         int customerID = 0;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try{
             String hql = "Select kh.maKhachHang from Khachhang kh Where taiKhoan=:taiKhoan";
             Query query = session.createQuery(hql);
             query.setString("taiKhoan", taiKhoan);
             Object objCustomerID = query.uniqueResult();
             customerID = (Integer)objCustomerID;
         }catch(HibernateException hex){
             System.err.print(hex);
         }finally{
             session.close();
         }
         return customerID;
     }
     //</editor-fold>
     
    //<editor-fold defaultstate="collapsed" desc="3. Đăng nhập khách hàng">
     public static Khachhang dangNhapKhachHang(String taiKhoan, String matKhau)
     {
         Khachhang khachhang = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try
         {
             String hql = "select kh from Khachhang kh where kh.taiKhoan=:taiKhoan and kh.matKhau=:matKhau";
             Query query = session.createQuery(hql);
             query.setString("taiKhoan", taiKhoan);
             query.setString("matKhau", matKhau);
             Object ob = query.uniqueResult();
             khachhang = (Khachhang)ob;
         }
         catch(HibernateException hex)
         {
             System.err.print(hex);
         }
         finally
         {
             session.close();
         }
         return khachhang;
     }
     //</editor-fold>
     
    //<editor-fold defaultstate="collapsed" desc="4. Cập nhật khách hàng">
     public static boolean updateCustomerInfo(Khachhang customerInfo)
     {
         Session session = HibernateUtil.getSessionFactory().openSession();
         Transaction tran = null;
         try
         {
             tran = session.beginTransaction();
             session.update(customerInfo);
         }
         catch(Exception hex)
         {
             System.err.print(hex);
             tran.rollback();
             return false;
         }
         finally 
         {
             tran.commit();
             session.close();
         }
         return true;
     }
     
     //</editor-fold>
     
    //<editor-fold defaultstate="collapsed" desc="5. Thêm khách hàng - register">
    public static boolean register(Khachhang khachhang) {
        boolean flag = true;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(khachhang);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            flag = false;
            System.err.println(ex);
        } finally {
            session.close();
        }
        return flag;
    }
     
     //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="6. Encrypt pass">
    public static String encryptPassword(String text){
        String encode = "";
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(text.getBytes());
            BigInteger dis = new BigInteger(1, md5.digest());
            encode = dis.toString(16);
        }catch(NoSuchAlgorithmException e){
            e.toString();
        }
        return encode;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="7. Kiểm tra trùng tên đăng nhập khách hàng khi đăng ký">
     public static boolean validateLogin(String taiKhoan)
     {
         boolean flag = false;
         Khachhang khachhang = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try
         {
             String hql = "select kh from Khachhang kh where kh.taiKhoan=:taiKhoan";
             Query query = session.createQuery(hql);
             query.setString("taiKhoan", taiKhoan);
             Object ob = query.uniqueResult();
             khachhang = (Khachhang)ob;
             if(khachhang!=null)
             {
                 flag = true;
             }
         }
         catch(HibernateException hex) {
             System.err.print(hex);
             return false;
         }
         finally{
             session.close();
         }
         return flag;
     }
     //</editor-fold>
    
}
